/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTROLLER;

import POJO.*;
import BUS.*;
import java.util.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;

/**
 * 
 * @author nhlvu07db373
 */
@Controller
public class GianhangController {

    private SanphamBUS spBUS;
    private GianhangBUS ghBUS;
    private KhuvucBUS kvBUS;
    private ThamsoBUS tsBUS;
    private HinhanhBUS haBUS;
    //Xử lý hiển thị danh sách sản phẩm

    @RequestMapping(value = "/quanligianhang")
    public ModelAndView gianhangShow(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        Map<String, Object> mv = new HashMap<String, Object>();
        //Do something...
        try {
            List lstKhuvuc = new ArrayList();
            List lstGianHang = new ArrayList();
            List lstSanPham = new ArrayList();
            lstGianHang = ghBUS.dsGianhangFi(1);
            if (lstGianHang.size() > 0) {
                for (int i = 0; i < lstGianHang.size(); i++) {
                    Gianhang gh = (Gianhang) lstGianHang.get(i);
                    Sanpham sp = spBUS.laySanpham(gh.getMaSanPham());
                    Khuvuc kv = kvBUS.getKhuVuc(gh.getMaKhuVuc());
                    lstKhuvuc.add(kv);
                    lstSanPham.add(sp);
                }
            }
            mv.put("lstGH", lstGianHang);
            mv.put("lstSP", lstSanPham);
            mv.put("lstKV", lstKhuvuc);
        } catch (Exception ex) {
            System.err.print(ex);
        }
        return new ModelAndView("quanligianhangview", mv);
    }
    //Xử lý cap nhat gian hang

    @RequestMapping(value = "/capnhatgianhang")
    public ModelAndView capnhatGhAction(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        Map<String, Object> mv = new HashMap<String, Object>();
        //Do something...
        try {
            Gianhang sp = ghBUS.layGianhang(request.getParameter("MaGH"));
            mv.put("gianHang", sp);
            mv.put("lstSP", spBUS.dsSanphamFi(1));
            mv.put("lstKV", kvBUS.dsKhuvucDAOFi(1));
        } catch (Exception ex) {
            System.err.print(ex);
        }
        return new ModelAndView("capnhatgianhangview", mv);
    }
    //Xử lý cap nhat gian hang

    @RequestMapping(value = "/xoagianhang")
    public ModelAndView xoaGhAction(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        Map<String, Object> mv = new HashMap<String, Object>();
        //Do something...
        try {
            String maGH = request.getParameter("MaGH");
            ghBUS.xoaGianhang(maGH);
            List lstKhuvuc = new ArrayList();
            List lstGianHang = new ArrayList();
            List lstSanPham = new ArrayList();
            lstGianHang = ghBUS.dsGianhangFi(1);
            if (lstGianHang.size() > 0) {
                for (int i = 0; i < lstGianHang.size(); i++) {
                    Gianhang gh = (Gianhang) lstGianHang.get(i);
                    Sanpham sp = spBUS.laySanpham(gh.getMaSanPham());
                    Khuvuc kv = kvBUS.getKhuVuc(gh.getMaKhuVuc());
                    lstKhuvuc.add(kv);
                    lstSanPham.add(sp);
                }
            }
            mv.put("lstGH", lstGianHang);
            mv.put("lstSP", lstSanPham);
            mv.put("lstKV", lstKhuvuc);
        } catch (Exception ex) {
            System.err.print(ex);
        }
        return new ModelAndView("quanligianhangview", mv);
    }
    //Xử lý tiep nhan gian hang moi

    @RequestMapping(value = "/tiepnhangianhang")
    public ModelAndView tiepnhatGhAction(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        Map<String, Object> mv = new HashMap<String, Object>();
        //Do something...
        try {
            mv.put("gianHang", new Gianhang());
            mv.put("lstSP", spBUS.dsSanphamFi(1));
            mv.put("lstKV", kvBUS.dsKhuvucDAOFi(1));
        } catch (Exception ex) {
            System.err.print(ex);
        }
        return new ModelAndView("capnhatgianhangview", mv);
    }
    //Xử lý thêm/cập nhật sản phẩm

    @RequestMapping(value = "submitgianhang", method = RequestMethod.POST)
    public String onSubmit(@ModelAttribute("gianHang") Gianhang _gh, BindingResult result,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        try {
            if (_gh != null) {
                ghBUS.luuGianhang(_gh);
            }

        } catch (Exception ex) {
            System.err.print(ex);
        }
        return "redirect:/quanligianhang.html";

    }

    /**
     * @param spBUS the spBUS to set
     */
    @Autowired
    public void setSpBUS(SanphamBUS spBUS) {
        this.spBUS = spBUS;
    }

    /**
     * @param ghBUS the ghBUS to set
     */
    @Autowired
    public void setGhBUS(GianhangBUS ghBUS) {
        this.ghBUS = ghBUS;
    }

    /**
     * @param kvBUS the kvBUS to set
     */
    @Autowired
    public void setKvBUS(KhuvucBUS kvBUS) {
        this.kvBUS = kvBUS;
    }

    @Autowired
    public void settsBUS(ThamsoBUS tsBUS) {
        this.tsBUS = tsBUS;
    }

    @Autowired
    public void sethaBUS(HinhanhBUS haBUS) {
        this.haBUS = haBUS;
    }

    @RequestMapping("/index2")
    public ModelAndView GianHanghow(HttpServletRequest request, HttpServletResponse response) throws Exception {

        response.setContentType("text/html;charset=UTF-8");
        Map<String, Object> mv = new HashMap<String, Object>();
        //Do something...

        try {

            List lstGianHang = null;
            ArrayList<Sanpham> lstSanPham = new ArrayList<Sanpham>();
            ArrayList<Hinhanh> lstHinhAnh = new ArrayList<Hinhanh>();

            lstGianHang = ghBUS.dsGianhangAll();
            if (lstGianHang != null) {
                for (int i = 0; i < lstGianHang.size(); i++) {
                    Gianhang gh = (Gianhang) lstGianHang.get(i);
                    Sanpham sp = spBUS.laySanpham(gh.getMaSanPham());
                    String maSp = sp.getMaSanPham();
                    HinhanhBUS a = new HinhanhBUS();
                    Hinhanh ha = a.layHinhDaiDien(maSp);
                    lstHinhAnh.add(ha);

                    lstSanPham.add(sp);
                }
            }
            List dsSanPham = (List) lstSanPham;
            List dsHinhAnh = (List) lstHinhAnh;


            Thamso ts = tsBUS.laySoLuongSanPhamTrenTrang();
            int soLuongSanPhamTrenTrang = ts.getSoSanPhamTrenTrang();
            mv.put("soLuong", soLuongSanPhamTrenTrang);
            mv.put("lstGianhang", ghBUS.dsGianhangAll());
            mv.put("lstSanpham", dsSanPham);
            mv.put("lstHinhAnh", dsHinhAnh);

        } catch (Exception ex) {
            System.err.print(ex);
        }

        return new ModelAndView("index2", mv);
    }

    @RequestMapping("/chitietsanpham")
    public ModelAndView sanphamShow(HttpServletRequest request, HttpServletResponse response) throws Exception {

        response.setContentType("text/html;charset=UTF-8");
        Map<String, Object> mv = new HashMap<String, Object>();
        //Do something...

        try {
            String tam = request.getParameter("Magh");
            Gianhang gh = ghBUS.layGianhang(tam);
            Sanpham sp = spBUS.laySanpham(gh.getMaSanPham());
            List dsha = haBUS.dsHinhSanPham(sp.getMaSanPham());



            mv.put("gianhang", gh);
            mv.put("sanpham", sp);
            mv.put("dshinhanh", dsha);

        } catch (Exception ex) {
            System.err.print(ex);
        }

        return new ModelAndView("chitietsanpham", mv);
    }
}
